package com.app.ui.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.ui.manager.TextViewManager;
import com.app.utiles.image.ImageUtile;
import com.wxj.zx.R;


/**
 * Created by Administrator on 2016/3/30.
 */
public class LoadingLayout extends RelativeLayout {
    public LoadingLayout(Context context) {
        super(context);
        init(context);
    }

    public LoadingLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public LoadingLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);

    }

    private ImageView loagdingFixationIv, loagdingTailorIv;
    private ImageView loagdingFailureIv;
    //没有内容，空
    private TextView loagdingEmptyTv;
    private int backgColor = 0xfff1f1f1;
    //true : 正在加载
    private boolean isloading;

    private void init(Context context) {
        setBackgroundColor(backgColor);
        //加入固定加载灰色图
        loagdingFixationIv = new ImageView(context);
        loagdingFixationIv.setImageResource(R.mipmap.loading_fixation);
        RelativeLayout.LayoutParams rlpFixation = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        rlpFixation.addRule(RelativeLayout.CENTER_IN_PARENT);
        addView(loagdingFixationIv, rlpFixation);

        //加入动态加载高亮图
        loadingBit = BitmapFactory.decodeResource(getResources(),
                R.mipmap.loading_tailor);
        loagdingTailorIv = new ImageView(context);
        loagdingTailorIv.setImageBitmap(loadingBit);
        RelativeLayout.LayoutParams rlpTailor = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        rlpTailor.addRule(RelativeLayout.CENTER_IN_PARENT);
        addView(loagdingTailorIv, rlpTailor);

        //加入加载失败的图
        loagdingFailureIv = new ImageView(context);
        loagdingFailureIv.setImageResource(R.mipmap.loading_failure);
        loagdingFailureIv.setBackgroundColor(backgColor);
        RelativeLayout.LayoutParams rlpFailure = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        rlpFailure.addRule(RelativeLayout.CENTER_IN_PARENT);
        addView(loagdingFailureIv, rlpFailure);

        loagdingFailureIv.setOnClickListener(new OnClick());
        //加载完后，空数据
        loagdingEmptyTv = new TextView(context);
        RelativeLayout.LayoutParams rlpEmpty = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        rlpEmpty.addRule(RelativeLayout.CENTER_IN_PARENT);
        loagdingEmptyTv.setGravity(Gravity.CENTER);
        loagdingEmptyTv.setTextColor(0xff999999);
        loagdingEmptyTv.setTextSize(14);
        loagdingEmptyTv.setCompoundDrawablePadding(10);
        loagdingEmptyTv.setOnClickListener(new OnClick());
        addView(loagdingEmptyTv, rlpEmpty);

    }

    //开始显示加载效果
    public void startLoading() {
        isloading = true;
        setLoadingState(1);
        handler.sendEmptyMessageDelayed(LOADING, 10);
    }

    //加载成功
    public void setLoadingSucceed() {
        isloading = false;
        handler.removeMessages(LOADING);
    }

    //加载失败
    public void setLoadingFailed() {
        isloading = false;
        setLoadingState(2);
        handler.removeMessages(LOADING);
    }

    //重新显示加载显示
    public void startRest() {
        if (isloading) {
            return;
        }
        isloading = true;
        setLoadingState(1);
        handler.sendEmptyMessageDelayed(LOADING, 5);
    }

    //加载完后，空数据
    public void setLoadingEmpty(boolean isClick) {
        isloading = false;
        emptyTvIsClick = isClick;
        handler.removeMessages(LOADING);
        setLoadingState(4);
    }


    private void setLoadingState(int state) {
        switch (state) {
            case 1:
                //要加载中的状态
                loagdingEmptyTv.setVisibility(View.GONE);
                loagdingFailureIv.setVisibility(View.GONE);
                loagdingFixationIv.setVisibility(View.VISIBLE);
                loagdingTailorIv.setVisibility(View.VISIBLE);
                break;
            case 2:
                //要加载失败的状态
                loagdingEmptyTv.setVisibility(View.GONE);
                loagdingFailureIv.setVisibility(View.VISIBLE);
                loagdingFixationIv.setVisibility(View.GONE);
                loagdingTailorIv.setVisibility(View.GONE);
                break;
            case 3:
                //要没数据的状态
                loagdingEmptyTv.setVisibility(View.VISIBLE);
                loagdingFailureIv.setVisibility(View.GONE);
                loagdingFixationIv.setVisibility(View.GONE);
                loagdingTailorIv.setVisibility(View.GONE);
                break;

        }
    }

    private boolean emptyTvIsClick = true;

    public void setEmptyContentNoClick(String content) {
        setEmptyContent(0, content, false);
    }

    public void setEmptyContentClick(String content) {
        setEmptyContent(0, content, true);
    }

    /***
     * @param iconResId 图片资源id
     * @param content 内容
     * @param isClick 是否响应点击事件
     */
    public void setEmptyContent(int iconResId, String content, boolean isClick) {
        if (iconResId != 0) {
            TextViewManager.setText(loagdingEmptyTv, iconResId, content, 1);
        } else {
            loagdingEmptyTv.setText(content);
        }
        emptyTvIsClick = isClick;
        isloading = false;
        setLoadingState(3);
    }

    class OnClick implements OnClickListener {

        @Override
        public void onClick(View v) {
            if (v == loagdingEmptyTv  && !emptyTvIsClick) {
                return;
            }
            if (isloading) {
                return;
            }
            //重新加载
            startRest();
            if (onResetLoagding == null) {
                return;
            }
            onResetLoagding.onResetRequest();
        }
    }


    // 加载中
    private final int LOADING = -1;
    /**
     * 当前百分数
     */
    private int num = 0;
    private Bitmap loadingBit;
    protected Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (!isloading) {
                return;
            }
            switch (msg.what) {
                case LOADING:
                    num++;
                    if (num > 100) {
                        num = 0;
                    }
                    loagdingTailorIv.setImageBitmap(ImageUtile.getRoundedCornerBitmap(
                            loadingBit, num));
                    handler.sendEmptyMessageDelayed(LOADING, 5);
                    break;
                default:

                    break;
            }
        }
    };
    private OnResetLoagding onResetLoagding;

    public void setOnResetLoagding(OnResetLoagding onResetLoagding) {
        this.onResetLoagding = onResetLoagding;
    }

    public interface OnResetLoagding {
        void onResetRequest();
    }
}
